<?php

namespace App\Models;

class TagData extends BaseModel
{

    protected $table = 'tag_datas';
    //主键ID
    protected $primaryKey = 'id';

    protected $fillable = ['tag_id', 'category_id', 'category_child_id', 'tag_name', 'data'];
    public static $tagCategory = [//标签分类
        1 => '业务属性',
        2 => '用户属性',
        3 => '行为属性',
    ];
    public static $tagCategoryChild = [ //标签分类之子项
        1 => [
            1 => '应用场景',
            2 => '生命周期',
            3 => '消费能力',
            4 => '用户价值',
        ],
        2 => [
            10 => '人口属性',
            11 => '社会属性',
            12 => '设备属性',
        ],
        3 => [
            20 => '访问行为',
            21 => '互助行为',
        ],
    ];
    public static $department = [//部门
        0 => '',
        1 => '大数据',
        2 => '轻松互助',
        3 => '轻松e保',
        4 => '大健康',
        5 => '微爱',
        6 => '轻松公益',
        7 => '市场部',
        8 => '其他',
    ];
    public static $tagState = [//标签状态
        0 => '',
        1 => '开发中',
        2 => '启用',
        3 => '停用',
    ];
    public static $openAuth = [//开放权限
        0 => '',
        1 => '轻松互助',
        2 => '轻松e保',
        3 => '微爱',
        4 => '轻松公益',
        5 => '市场部',
        6 => '外部',
    ];
    public static $tagType = [//标签类型
        0 => '',
        1 => '值',
        2 => '类型',
    ];
    public static $tagValueUnit = [//标签值单位
        0 => '空',
        1 => '元',
        2 => '印尼盾',
        3 => '数量',
        4 => '岁',
    ];
    public static $tagPhysicsType = [//标签物理类型
        0 => '',
        1 => 'string',
        2 => 'bigint',
        3 => 'datetime',
        4 => 'double',
    ];

    /**
     * get type value
     * @param $type
     * @param $id
     * @return mixed|string
     */
    public static function changeType($type, $id) {
        switch ($type) {
            case 'department':
                return isset(self::$department[$id]) ? self::$department[$id] : '';
                break;
            case 'state':
                return isset(self::$tagState[$id]) ? self::$tagState[$id] : '';
                break;
            case 'auth':
                return isset(self::$openAuth[$id]) ? self::$openAuth[$id] : '';
                break;
            case 'type':
                return isset(self::$tagType[$id]) ? self::$tagType[$id] : '';
                break;
            case 'unit':
                return isset(self::$tagValueUnit[$id]) ? self::$tagValueUnit[$id] : '';
                break;
            case 'physics':
                return isset(self::$tagPhysicsType[$id]) ? self::$tagPhysicsType[$id] : '';
                break;
            default:
                return '';
                break;
        }
    }
    /**
     * 获取分类子项目
     * @param int $parent_id
     * @return array|bool|mixed
     */
    public static function getCategoryChild($parent_id = 0) {
        if ($parent_id <= 0) {
            $result = [];
            foreach (self::$tagCategoryChild as $item) {
                foreach ($item as $key => $val) {
                    $result[$key] = $val;
                }
            }
            return $result;
        } else {
            if(isset(self::$tagCategoryChild[$parent_id])) {
                return self::$tagCategoryChild[$parent_id];
            }
            return false;
        }
    }

    /**
     * 获取自增tag_id的下一个ID
     * @return int
     */
    public function getNextTagId(){
        $max = self::max('tag_id');
        if(empty($max)) {
            return 10000;
        }
        return $max + 1;
    }

    /**
     * 判断名称 是否存在
     * @param $name
     * @return bool
     */
    public function checkTagNameUnique($name) {
        $count = self::where('tag_name', $name)->count();
        if($count > 0) {
            return true;
        }
        return false;
    }
    /**
     * 判断名称 是否存在2
     * @param $id
     * @param $name
     * @return bool
     */
    public function checkTagNameUnique2($id, $name) {
        $count = self::where('id', '<>', $id)->where('tag_name', $name)->count();
        if($count > 0) {
            return true;
        }
        return false;
    }

    /**
     * 获取分类
     * @param $id
     * @param $childId
     * @return string
     */
    public static function getCategoryMix($id, $childId){
        if (intval($id) == 0) {
            $str = implode(',', self::$tagCategory);
        } else {
            $str = self::$tagCategory[$id];
        }
        if (intval($childId) == 0) {
            $arr = [];
            foreach (self::$tagCategoryChild as $item) {
                foreach ($item as $val) {
                    $arr[] = $val;
                }
            }
            $child_str = implode(',', $arr);
        } else {
            $child_str = isset(self::$tagCategoryChild[$id][$childId]) ? self::$tagCategoryChild[$id][$childId] : '';
        }
        return $str . '——' . $child_str;
    }
}
